package com.chrray.position.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LTB
 * @date 2022/7/10
 */
public enum YesNoEnum {
    NO(Integer.valueOf(0), "否"),
    YES(Integer.valueOf(1), "是");

    YesNoEnum(Integer code, String value) {
        this.code = code;
        this.value = value;
    }

    @EnumValue
    @JsonValue
    private Integer code;

    private String value;

    public Integer getCode() {
        return this.code;
    }

    public String getValue() {
        return this.value;
    }

    public static String getValue(Integer code) {
        if (null == code)
            return null;
        List<YesNoEnum> collect = (List<YesNoEnum>)Arrays.<YesNoEnum>stream(values()).filter(e -> e.getCode().equals(code)).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect))
            return null;
        YesNoEnum yesNoEnum = collect.get(0);
        return yesNoEnum.getValue();
    }


    public static YesNoEnum getEnum(Integer code) {
        if (null == code)
            return null;
        List<YesNoEnum> collect = (List<YesNoEnum>)Arrays.<YesNoEnum>stream(values()).filter(e -> e.getCode().equals(code)).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect))
            return null;
        YesNoEnum yesNoEnum = collect.get(0);
        return yesNoEnum;
    }

    public static YesNoEnum getByInfo(String info) {
        for (YesNoEnum en : YesNoEnum.values()) {
            if (en.getValue().equals(info)) {
                return en;
            }
        }
        return null;
    }
}
